<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
      <?php foreach ($queryList as $val) {?>
        <?php if (isset($val['columnValue'])) {?>
          <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

          <?php } elseif ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>

          <el-col :lg="6" :md="12">
            <el-form-item label="<?php echo $val['columnComment']?>:">
              <el-select
                clearable
                v-model="where.<?php echo $val['columnName']?>"
                placeholder="请选择<?php echo $val['columnComment']?>"
                class="ele-fluid">
              <?php foreach ($val['columnValue'] as $v) { ?>
                <el-option label="<?php echo $v["name"]?>" value="<?php echo $v["value"]?>"/>
              <?php } ?>
              </el-select>
            </el-form-item>
          </el-col>
          <?php } ?>
        <?php } else {?>

          <el-col :lg="6" :md="12">
            <el-form-item label="<?php echo $val['columnComment']?>:">
              <el-input
                clearable
                v-model="where.<?php echo $val['columnName']?>"
                placeholder="请输入<?php echo $val['columnComment']?>"/>
            </el-form-item>
          </el-col>
        <?php } ?>
      <?php } ?>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:<?php echo $moduleName?>:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:<?php echo $moduleName?>:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:<?php echo $moduleName?>:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此<?php echo $moduleTitle?>吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:<?php echo $moduleName?>:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
    <?php foreach ($columnList as $val) {?>
      <?php if ($val['columnName'] != 'id' && $val['columnName'] != 'create_user' && $val['columnName'] != 'create_time' && $val['columnName'] != 'update_time' && $val['columnName'] != 'mark') {?>
        <?php if (isset($val['columnValue'])) {?>
        <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

          <!-- <?php echo $val['columnComment']?>列 -->
          <template slot="<?php echo $val['columnName']?>" slot-scope="{row}">
            <el-switch
              v-model="row.<?php echo $val['columnName']?>"
              @change="<?php echo $val['columnSwitchName']?>(row)"
              :active-value="1"
              :inactive-value="2"/>
          </template>
        <?php } elseif ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>

          <!-- <?php echo $val['columnComment']?>列 -->
          <template slot="<?php echo $val['columnName']?>" slot-scope="{row}">
          <?php foreach ($val['columnValue'] as $v) { ?>
            <el-tag v-if="row.<?php echo $val['columnName']?> === <?php echo $v["value"]?>" type="<?php echo $v["type"]?>" size="small"><?php echo $v["name"]?></el-tag>
          <?php } ?>
          </template>
        <?php } ?>
        <?php } elseif (isset($val['columnImage']) && $val['columnImage']) {?>
          <!-- <?php echo $val['columnComment']?>列 -->
          <template slot="<?php echo $val['columnName']?>" slot-scope="{row}">
            <el-avatar shape="square" :size="25" :src="row.<?php echo $val['columnName']?>"/>
          </template>
        <?php } else {?>
        
        <?php } ?>
      <?php } ?>
    <?php } ?>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <<?php echo $moduleName?>-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import <?php echo $entityName?>Edit from './<?php echo $moduleName?>-edit';

export default {
  name: 'Exam<?php echo $entityName?>',
  components: {<?php echo $entityName?>Edit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/<?php echo $moduleName?>/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
<?php foreach ($columnList as $val) {?>
  <?php if ($val['columnName'] != 'id' && $val['columnName'] != 'create_user' && $val['columnName'] != 'create_time' && $val['columnName'] != 'update_time' && $val['columnName'] != 'mark') {?>
    <?php if (isset($val['columnValue'])) {?>
      <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>
        {
          prop: '<?php echo $val['columnName']?>',
          label: '<?php echo $val['columnComment']?>',
          align: 'center',
          width: 100,
          resizable: false,
          slot: '<?php echo $val['columnName']?>',
        },
      <?php } elseif ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>
        {
          prop: '<?php echo $val['columnName']?>',
          label: '<?php echo $val['columnComment']?>',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: '<?php echo $val['columnName']?>',
        },
      <?php } ?>
    <?php } elseif (isset($val['columnImage']) && $val['columnImage']) {?>
        {
          columnKey: '<?php echo $val['columnName']?>',
          label: '<?php echo $val['columnComment']?>',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: '<?php echo $val['columnName']?>'
        },
    <?php } else {?>
        {
          prop: '<?php echo $val['columnName']?>',
          label: '<?php echo $val['columnComment']?>',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
    <?php } ?>
  <?php } ?>
<?php } ?>
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/<?php echo $moduleName?>/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的<?php echo $moduleTitle?>吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/<?php echo $moduleName?>/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  <?php foreach ($columnList as $val) {?>
    <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

    /* 更改<?php echo $val['columnComment']?> */
    <?php echo $val['columnSwitchName']?>(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "<?php echo $val['columnName']?>": row.<?php echo $val['columnName']?>
      })
      this.$http.post('/<?php echo $moduleName?>/<?php echo $val['columnSwitchName']?>', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.<?php echo $val['columnName']?> = !row.<?php echo $val['columnName']?> ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    <?php } ?>
  <?php } ?>
  }
}
</script>

<style scoped>
</style>
